<script src="../../../config/index.js"></script>
<template>
  <div class="app-container">
    <el-card class="box-card">
      <!-- <el-button type="primary" size="default" @click="handleAdd">新增</el-button> -->

      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        border
        class="mt-3"
        stripe
        style="width: 100%"
        tooltip-effect="dark"
      >
        <el-table-column :index="indexMethod" label="序号" prop="user_code" type="index" width="100px"></el-table-column>
        <el-table-column label="萌宠品质" prop="name"></el-table-column>
        <el-table-column label="粉钻数量/小时" prop="desc"></el-table-column>
        <el-table-column label="获得萌宠碎片数量" prop="need"></el-table-column>
        <el-table-column label="萌宠碎片概率/小时" prop="max_count">
          <template slot-scope="{row}">
            {{ row.max_count === -1 ? '无限次' : row.max_count }}
          </template>
        </el-table-column>
        <el-table-column label="获得礼物" prop="is_open"></el-table-column>
        <el-table-column label="获得礼物概率" prop="created_at"></el-table-column>
        <el-table-column label="编辑时间" prop="created_at"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(scope.row)"
            >编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <cus-pagination
        v-show="total > 0"
        :limit.sync="listQuery.perPage"
        :page.sync="listQuery.page"
        :total="total"
        @pagination="getTableData"
      />
    </el-card>

    <el-dialog
      :visible.sync="dialogVisible"
      title="编辑"
      width="30%"
      @close="closeDialog('form')"
    >

      <el-form ref="form" :inline="false" :model="formData" :rules="rules" label-width="150px" size="normal">
        <el-form-item label="萌宠品质" prop="name">
          <el-input v-model="formData.name" disabled placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="粉钻数量/小时" prop="price">
          <el-input v-model="formData.price" clearable placeholder="请输入粉钻数量/小时" size="normal">
            <template slot="append">个</template>
          </el-input>
        </el-form-item>
        <el-form-item label="碎片数量最小值" prop="price">
          <el-input v-model="formData.price" clearable placeholder="请输入碎片数量最小值" size="normal">
            <template slot="append">个</template>
          </el-input>
        </el-form-item>
        <el-form-item label="碎片数量最大值" prop="price">
          <el-input v-model="formData.price" clearable placeholder="请输入碎片数量最大值" size="normal">
            <template slot="append">个</template>
          </el-input>
        </el-form-item>
        <el-form-item label="获得碎片概率" prop="price">
          <el-input v-model="formData.price" clearable placeholder="请输入道具价值" size="normal">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="礼物ID" prop="price">
          <el-input v-model="formData.price" clearable placeholder="请输入礼物ID" size="normal">
            <template slot="append">
              <div @click="handleSynchronization">同步</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="礼物名称" prop="price">
          <el-input v-model="formData.price" disabled placeholder="礼物名称" size="normal"></el-input>
        </el-form-item>
        <el-form-item label="礼物价值" prop="price">
          <el-input v-model="formData.price" disabled placeholder="礼物价值" size="normal">
            <template slot="append">M豆</template>
          </el-input>
        </el-form-item>
        <el-form-item label="获得礼物概率" prop="price">
          <el-input v-model="formData.price" clearable placeholder="请输入获得礼物概率" size="normal">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleAllocationSkill">确定</el-button>
            </span>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogVisible2"
      title="基础配置"
      width="30%"
      @close="closeDialog('form2')"
    >

      <el-form ref="form" :inline="false" :model="formData2" :rules="rules2" label-width="150px" size="normal">
        <el-form-item label="8小时打工开始时间" prop="name">
          <el-time-picker
            v-model="formData2.firstTime"
            value-format="HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="8小时打工结束时间" prop="price">
          <el-time-picker
            v-model="formData2.lastTime"
            value-format="HH:mm:ss"
          />
        </el-form-item>

      </el-form>

      <span slot="footer">
          <el-button @click="dialogVisible2 = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { balanceTaskList, balanceTaskSave } from '@/api/petsManagement'

export default {
  name: 'workSetting',
  data() {
    return {
      formData2: {},
      dialogVisible2: true,
      checked: false,
      dialogTitle: 'add',
      dialogVisible: false,
      info: {},
      loading: false,
      loading2: false,
      tableData: [],
      listQuery: {
        page: 1,
        perPage: 10
      },
      total: 1,
      options: [
        {
          value: 'gift',
          label: '礼物'
        },
        {
          value: 'pet-prop',
          label: '宠物道具'
        },
        {
          value: 'product',
          label: '装扮'
        }
      ],
      formData: {
        id: undefined,
        name: undefined,
        desc: undefined,
        need: undefined,
        max_count: undefined,
        is_open: 1
      },
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        reward_id: [{ required: true, message: '请选择', trigger: 'blur' }]
        // coin: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      query: undefined
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {

        }
      })
    },
    indexMethod(index) {
      return this.total - (this.listQuery.page - 1) * this.listQuery.perPage - index
    },
    // 网络请求
    /**
     * getTableData
     * @desc 获取表格数据
     */
    async getTableData() {
      this.loading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key] == 0 ? 0 : this.listQuery[key]
      }
      const { data, code } = await balanceTaskList(this.listQuery)
      if (code === 200) {
        this.tableData = data.list
        this.total = data.meta.total
      }
      this.loading = false

    },
    closeDialog(form) {
      if (this.$refs[form]) {
        this.$refs[form].clearValidate() // 清除整个表单的校验
        this.$refs[form].resetFields() // 清除整个表单的校验
      }
      this.formData = {
        id: undefined,
        name: undefined,
        desc: undefined,
        need: undefined,
        max_count: undefined,
        is_open: 1
      }

      this.formData2 = {}

    },
    handleEdit(row) {
      this.dialogVisible = true
      this.formData = JSON.parse(JSON.stringify(row))
      this.formData.reward_id = Number(this.formData.reward_id)
    },
    handleAdd() {
      this.dialogVisible = true
    },
    handleDel(row) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        petPrizeDelete({
          id: row.id
        }).then(res => {
          if (res.code === 200) {
            this.dialogVisible = false
            this.$message({
              type: 'success',
              message: '操作成功'
            })
          }
          this.getTableData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    handleAllocationSkill() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          for (let key in this.formData) {
            this.formData[key] = !this.formData[key] ? undefined : this.formData[key]
          }
          balanceTaskSave(this.formData).then(res => {
            if (res.code === 200) {
              this.dialogVisible = false
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.dialogVisible = false
              this.getTableData()
            }
          })
        }
      })
    },
    handleSynchronization() {
      console.log(111)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .hide_box {
    .el-upload--picture-card {
      display : none;
    }
  }

  .el-upload-list__item {
    margin-right : 1em !important;
  }
}

.image {
  width : 200px;
}

::v-deep {
  .formItemBox {
    .el-form-item {
      width : 50% !important;
    }
  }

}

</style>
